package com.mycompany.oj.Mapper.Impl;

import com.mycompany.oj.Mapper.QuestionJudgeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author wucy
 */
@Component
public class MyQuestionJudgeMapperImpl implements QuestionJudgeMapper {

    @Autowired
    SqlOperator sqlOperator;

    //蓝桥题库初始化
    public List<Map<String, Object>> initQuestionMapper(String kind) {
        String stmt = "select id,question_name,lev from Init_questionbank where Kind_id=? ";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, kind);
        return temp;
    }

    //加载具体题目
    public List<Map<String, Object>> initOneQuestionMapper(String id) {
        String stmt = "select question_name,question_text,lev,type from Init_question where id=?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, id);
        return temp;
    }

    //获取account的主键
    public List<Map<String, Object>> getAccountId(String username) {
        String stmt = "select id from Account where username=?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, username);
        return temp;
    }
    public List<Map<String, Object>> getAccountIdbyschoolid(String schoolid) {
        String stmt = "SELECT Account.id from Account,Student where Account_id = Account.id and schoolid = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, schoolid);
        return temp;
    }
    
    
    //获取收藏夹id
    public List<Map<String, Object>> getFavId(String username) {
        String stmt = "SELECT id from My_favorites where Student_id  = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, username);
        return temp;
    }

    //添加到收藏夹
    public String[] favoriteMapper(int student_id, int question_id, int i) {
        String stmt = "insert into StudentFavorites(Student_id,question_date,Question_id,my_favorites_id) values(?,now(),?,?)";
        String[] temp = sqlOperator.insert(stmt, student_id, question_id, i);
        return temp;
    }

    //代码运行结果
    public List<Map<String, Object>> resultMapper(String schoolid) {
        String stmt = "select * from Find_register where student_id= ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, schoolid);
        return temp;
    }

    //获取题目答案
    public List<Map<String, Object>> getQuestion(String id) {
        String stmt = "select * from Question_answer where Question_id = ?";
        List<Map<String, Object>> temp1 = sqlOperator.query(stmt, id);
        return temp1;
    }
    //获取实验题目答案
     public List<Map<String, Object>> getCourseQuestionAnswer(String id) {
        String stmt = "SELECT standard_answer answer FROM Question_course WHERE id = ?";
        List<Map<String, Object>> temp1 = sqlOperator.query(stmt, id);
        return temp1;
    }

    //计算蓝桥杯题库某道题的通过率
    public List<Map<String, Object>> calculateAC(String id) {
        String stmt1 = "select number from Post_number where question_id = ?";
        String stmt2 = "select number from Pass_number where question_id = ?";
        List<Map<String, Object>> temp1 = sqlOperator.query(stmt1, id);
        Map<String, Object> e = new HashMap<>();
        e.put("number", "0");
        if (temp1.isEmpty()) {
            temp1.add(e);
        }
        List<Map<String, Object>> temp2 = sqlOperator.query(stmt2, id);
        if (temp2.isEmpty()) {
            temp1.add(e);
        } else {
            temp1.add(1, temp2.get(0));
        }
        return temp1;
    }

    //计算实验某道题的通过率
//    public List<Map<String, Object>> calculate_course_AC(String id) {
//        String stmt1 = "select number from Course_post_number where id = ?";
//        String stmt2 = "select number from Course_pass_number where id = ?";
//        List<Map<String, Object>> temp1 = sqlOperator.query(stmt1, id);
//        Map<String, Object> e = new HashMap<>();
//        e.put("number", "0");
//        if (temp1.isEmpty()) {
//            temp1.add(e);
//        }
//        List<Map<String, Object>> temp2 = sqlOperator.query(stmt2, id);
//        if (temp2.isEmpty()) {
//            temp1.add(e);
//        } else {
//            temp1.add(1, temp2.get(0));
//        }
//        return temp1;
//    }

    //获取学生完成各个实验的题数,要先获取student主键
    @Override
    public List<Map<String, Object>> haveFinishedNumber(String username, String course_name) {
        String stmt = "Select number,section_num from Finished_number where Student_id = ? and name =?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, username, course_name);
        return temp;
    }

    //获取实验总题数section_num是章节号,section_number是个数
    @Override
    public List<Map<String, Object>> totalNumber(String course_name) {
        String stmt = "Select section_num,section from Section_total_number where name = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, course_name);
        return temp;
    }
    

    //获取课程下的所有实验题目
    @Override
    public List<Map<String, Object>> allQuestion(String course_name) {
        String stmt = "Select section_num,question_name,id from Section_question where name = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, course_name);
        return temp;
    }

    //获取最后一次提交时间,要先获取student主键
    @Override
    public List<Map<String, Object>> getLastTime(String username, String question_course_id) {
        String stmt = "Select question_course_id,last_time from Get_post_time where Student_id = ? and name = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, username, question_course_id);
        return temp;
    }


    //获取章节信息
    public List<Map<String, Object>> getZhangjieInformation(String course_name) {
        String stmt = "Select section_name,section_gogal,section_end_time,section_num from Section_information where name = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, course_name);
//        System.err.println("Mapper"+temp.size());
        return temp;
    }

    //获取章节数
    public List<Map<String, Object>> getZhangjieNumber(String course_name) {
        String stmt = "Select number from Section_number where Coursename = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, course_name);
        return temp;
    }

    //实验页面目前题目,先转化
    public List<Map<String, Object>> lastCourseAddressMapper(String username) {
        String stmt = "Select question_name,question_course_id from Course_address where student_id = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, username);
        return temp;
    }

    //题库页面目前题目
    public List<Map<String, Object>> lastAddressMapper(String username) {
        String stmt = "Select question_name,question_id from Question_address where student_id = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, username);
        return temp;
    }

    //获取所有已经通过的题目
    public List<Map<String, Object>> getAllPassQuestion(String username) {
        String stmt = "Select Question_course_id from Get_True_Register where student_id = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, username);
        return temp;
    }

    @Override
    public List<Map<String, Object>> getCoursenameEndData(String course_id) {
        String stmt = "SELECT section_end_time,Course.name FROM Section,Course,Question_course WHERE Course_id = Course.id AND Section_id = Section.id AND Question_course.id = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, course_id);
        return temp;
    }

    @Override
    public List<Map<String, Object>> getFirstId() {
        String stmt = "Select question_name,id from Question_course where Section_id = 1 LIMIT 1";
        List<Map<String, Object>> temp = sqlOperator.query(stmt);
        return temp;
    }

    @Override
    public List<Map<String, Object>> getFirstCourse(String course_name) {
        String stmt = "SELECT min(Section.id),any_value(section_end_time) section_end_time from Section,Course where Course_id = Course.id AND Course.`name` = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, course_name);
        return temp;
    }

    public List<Map<String, Object>> getStudentCourseFinishedNumber(String course_name, String username) {
        String stmt = "SELECT number from Student_finished_Course_number where Account_id = ? and name = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, username, course_name);
        return temp;
    }

    //获取name
    public List<Map<String, Object>> getQuestionName(String question_id) {
        String stmt = "Select question_name from Question_course where id = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, question_id);
        return temp;
    }

    //判断是否收藏过
    public List<Map<String, Object>> ifFavortite(String username, String question_id) {
        String stmt = "Select * from StudentFavorites where Student_id = ? and Question_id = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, username, question_id);
        return temp;
    }

    public List<Map<String, Object>> getStudentId(String username) {
        String stmt = "Select Student.id from Student,Account where Account.username = ? AND Student.Account_id = Account.id";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, username);
        return temp;
    }
    
    //每个人每课程完成数
    public List<Map<String,Object>> getEveryQuestion(String account_id, String course_name){
        String stmt = "Select number from Every_Student_do_Course_number where Account_id = ? and name = ?";
        List<Map<String,Object>> temp = sqlOperator.query(stmt,account_id, course_name);
        return temp;
    }
    //每门课的题数
    public List<Map<String,Object>> getCourseNum(String course_name){
        String stmt = "Select number from Question_Course_total_number where coursename =?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, course_name);
        return temp;
    }
    //最后的课程
    public List<Map<String,Object>> getLastCourse(String Account_id){
        String stmt = "Select name from Last_course where Account_id = ?";
        List<Map<String,Object>> temp = sqlOperator.query(stmt, Account_id);
        return temp;
    }
    //获取username
     public List<Map<String,Object>> getUsername(String schoolid){
        String stmt = "Select username from Account,Student where Account_id = Account.id and schoolid = ?";
        List<Map<String,Object>> temp = sqlOperator.query(stmt, schoolid);
        return temp;
    } 
    
     public List<Map<String,Object>> getCourseQuestion(String id){
         String stmt = "Select question_text,question_name from Question_course where id = ?";
         List<Map<String, Object>> temp = sqlOperator.query(stmt, id);
         return temp;
     }
    

}
